package com.artPreter.controller;

import com.artPreter.pojo.Result;
import com.artPreter.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Tag(name = "文件接口")
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileController {


    @Autowired
    private FileService fileService;

    @PostMapping("/upload/t")
    @Operation(summary = "艺术家封面文件上传")
    public Result<String> uploadTFile(
            @Parameter(description ="表单文件对象") @RequestParam(value = "file") MultipartFile file
    ) {
        String path = "";
        if(file.getContentType().equals("image/png")||file.getContentType().equals("image/jpg")||file.getContentType().equals("image/jpeg")){
            String type = "/t";
            path =fileService.uploadFile(file,type);
        }else{
            path = "上传文件类型不正确,请检查后上传";
            return Result.error(path);
        }

        return Result.success(path);
    }

    @PostMapping("/upload/d")
    @Operation(summary = "机构封面文件上传")
    public Result<String> uploadDFile(
            @Parameter(description ="表单文件对象") @RequestParam(value = "file") MultipartFile file
    ) {
        String path = "";
        if(file.getContentType().equals("image/png")||file.getContentType().equals("image/jpg")||file.getContentType().equals("image/jpeg")){
            String type = "/d";
            path =fileService.uploadFile(file,type);
        }else{
            path = "上传文件类型不正确,请检查后上传";
            return Result.error(path);
        }

        return Result.success(path);
    }

    @PostMapping("/upload/p")
    @Operation(summary = "图片文件上传")
    public Result<String> uploadPFile(
            @Parameter(description ="表单文件对象") @RequestParam(value = "file") MultipartFile file
    ) {
        String path = "";
        if(file.getContentType().equals("image/png")||file.getContentType().equals("image/jpg")||file.getContentType().equals("image/jpeg")){
            String type = "/p";
            path =fileService.uploadFile(file,type);
        }else{
            path = "上传文件类型不正确,请检查后上传";
            return Result.error(path);
        }

        return Result.success(path);
    }

    @PostMapping("/upload/w")
    @Operation(summary = "图片文件上传")
    public Result<String> uploadWFile(
            @Parameter(description ="表单文件对象") @RequestParam(value = "file") MultipartFile file
    ) {
        String path = "";
        if(file.getContentType().equals("image/png")||file.getContentType().equals("image/jpg")||file.getContentType().equals("image/jpeg")){
            String type = "/w";
            path =fileService.uploadFile(file,type);
        }else{
            path = "上传文件类型不正确,请检查后上传";
            return Result.error(path);
        }

        return Result.success(path);
    }

    @PostMapping("/upload/v")
    @Operation(summary = "视频文件上传")
    public Result<String> uploadVFile(
            @Parameter(description ="表单文件对象") @RequestParam(value = "file") MultipartFile file
    ) {
        String path = "";
        System.out.println("上传图片"+file.getContentType());
        if(file.getContentType().equals("video/mp4")){
            String type = "/v";
            path =fileService.uploadVideoFile(file,type);
        }else{
            path = "上传文件类型不正确,请检查后上传";
            return Result.error(path);
        }

        return Result.success(path);
    }


}
